import OniApi from 'oni-api-creator';
import router from 'umi/router';
import { message } from 'antd';
import auth from './auth';
import base from './base';
import weightSchema from './weight';
import region from './region';
import customer from './customer';
import supplier from './supplier';
import user from './user';
import service from './service';
import product from './product';
import notification from './notification';
import price from './price';
import bill from './bill';
import order from './order';
import orderXq from './orderXq';
import warehouse from './warehouse';
import role from './role';
import org from './org';
import usergroups from './usergroups';
import balance from './balance';
import accountmanage from './accountmanage';
import credit from './credit';
import dashboard from './dashboard';
import suggestion from './suggestion';
import notice from './notice';
import sourceSide from './sourceSide';
import sourceManage from './sourceManage';
import hui from './hui';
import resource from './resource';
import sign from './sign';
import cabin from './cabin';
import afterSale from './afterSale';
import productService from './productService';
import financialAfterSale from './financialAfterSale';
import insurance from './insurance';
import member from './member';
import beanRule from './beanRule';
import signRule from './signRule';
import specialline from './specialline';
import customerRule from './customerRule';
import cashCoupon from './cashCoupon';
import buyRule from './buyRule';
import memberReport from './memberReport';
import cooperativeStore from './cooperativeStore';
import address from './address';
// added by xiong
import info from './info'

import sysSet from './sysSet';
import app from './app';
import add from './add';

OniApi.interceptors.response.use(
  res => {

    // if (res.data.code !== 200 && res.data.errorCode !== 0) {
    //   if (res.data.errorCode === 401) {
    //     window.g_app._store.dispatch({
    //       type: 'auth/logout',
    //     });
    //     router.push(`/auth/login`);
    //     return {};
    //   }
    //   message.error(res.data.msg, 10);
    //   if (res.data.errorCode === 500) {
    //     router.push('/exception/500');
    //     return {};
    //   }
    //   return res.data;
    // }
    return res.data
  },
  error => {
    const { response } = error;
    if (response) {
      errorHandle(response.status, response.data.msg);
      return Promise.reject(response);
    }
  }
);

/**
 * 请求失败后的错误统一处理
 * @param {Number} status 请求失败的状态码
 */
const errorHandle = (status, other) => {
  message.destroy();
  switch (status) {
    case 500:
      // router.push('/exception/500');
      message.error('系统繁忙，请稍后再试', 10);
      break;
    case 501:
      message.error('系统繁忙，请稍后再试', 10);
      break;
    case 502:
      message.error('系统繁忙，请稍后再试', 10);
      break;
    case 503:
      message.error('系统繁忙，请稍后再试', 10);
      break;
    case 504:
      message.error('系统繁忙，请稍后再试', 10);
      break;
    case 505:
      message.error('系统繁忙，请稍后再试', 10);
      break;
    default:
      message.error(other, 10);
  }
};

const api = {
  auth,
  base,
  weightSchema,
  region,
  customer,
  supplier,
  user,
  service,
  product,
  notification,
  price,
  bill,
  order,
  orderXq,
  warehouse,
  role,
  org,
  usergroups,
  balance,
  accountmanage,
  credit,
  dashboard,
  sysSet,
  app,
  suggestion,
  notice,
  sourceSide,
  sourceManage,
  hui,
  resource,
  sign,
  cabin,
  afterSale,
  productService,
  financialAfterSale,
  insurance,
  member,
  beanRule,
  signRule,
  specialline,
  cashCoupon,
  customerRule,
  buyRule,
  memberReport,
  cooperativeStore,
  info,
  add,
  address
};

OniApi.use(api);

export default api;
